Custom parameter need in createuser member in Custom Membership Provider in asp.net - asp.net-membership

I want to customize the parameter in createuser() method Membership Provider,
Actually, i have my own data store for users with different data including username,password.
but, the createuser() is not suite with my data
Any one can help me!

You can do something like this.
using System;
namespace SampleApplication.Models
{
using System.Web.Security;
public class SampleMembershipUser : MembershipUser
{
public int UserLevelId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ApplicationName { get; set; }
public Guid UserId { get; set; }
public TASMembershipUser(
string providername,
string username,
object providerUserKey,
string email,
string passwordQuestion,
string comment,
bool isApproved,
bool isLockedOut,
DateTime creationDate,
DateTime lastLoginDate,
DateTime lastActivityDate,
DateTime lastPasswordChangedDate,
DateTime lastLockedOutDate,
int userLevelId,
string firstName,
string lastName,
string applicationName,
Guid userId) :
base(
providername,
username,
providerUserKey,
email,
passwordQuestion,
comment,
isApproved,
isLockedOut,
creationDate,
lastLoginDate,
lastActivityDate,
lastPasswordChangedDate,
lastLockedOutDate)
{
UserLevelId = userLevelId;
FirstName = firstName;
LastName = lastName;
ApplicationName = applicationName;
UserId = userId;
}
}
}

As long as your provider is the only one in use, you don't need to call your CreateUser method through ASP.NET at all. Just create the method anywhere and call it normally.

Michael is correct just thought that I would add some resources that helped me to undertake this task:
Great resource from Microsoft:
How to: Implement a Custom Membership User
The other key thing to remember that is mentioned in the above link is that once you have done the work to create custom membership user and overloads for your methods like CreateUser is that where you call Membership.CreateUser you need to cast this to your custom membership provider like:
((CustomMembershipProvider)Membership.Provider).CreateUser(p1,p2 etc...)

Related

How to pass DateTime object as json in .net web api

I Have a model which has DateTime type in it:
public class ToDo
{
public int id { get; set; }
public int parentId { get; set; }
public string note { get; set; }
public DateTime due { get; set; }
public ToDo(int id, int parentId, string note, DateTime due)
{
this.id = id;
this.parentId = parentId;
this.note = note;
this.due = due;
}
}
I've created a controller for this class to send my post requests through api. but I don't know how to bind DateTime type to json i've tried a request with the following body but it didn't work out:
{"parentId":1,"note":"hello world","due":{"year":2017,"month": 11,"day":25}}
How should I post the DateTime type?
Apparently one of the ways you can do it is this:
{"due": "2017-11-01T00:00:00"}
it was actually an easy question but if you want to make sure how to make a proper post request for unknown object types format it's best to send an object with empty body to see the default values.
For DateTime Type property, you need to pass the String which could be converted to DateTime Type.
For {"year":2017,"month": 11,"day":25}, it is object instead of String, it will fail to convert to DateTime.
For anything which could be converted to DateTime by Convert.ToDateTime and DateTime.Parse.
So, both {"parentId":1,"note":"hello world","due":"05/05/2005"} and {"parentId":1,"note":"hello world","due":"2018-05-10"} will work, you could make test with the DateTime string you need.

Dapper - Query multiple models

My query involves multiple tables and from what I've read on Dapper, I can only find examples, that I understand at least, that query one model.
Below are my 3 classes under the Models folder:
public class User
{
public string UserName { get; set; }
public string UserId { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
public class Date
{
public string UserName { get; set; }
public string UserCode { get; set; }
public string LastLogin { get; set; }
}
public class Photo
{
public class UserName { get; set; }
public string UserId { get; set; }
public string PhotoUrl { get; set; }
}
In my repository I have my connection code and then a method to get all the information I need, however this method is tied to the User model only but I also need to retrieve the photo and when I try to make a compound class so I can the User and Photo models in the view, it gives me an error saying it expects only the User DataView.
public List<User> GetAll()
{
using (SqlConnection cn = new SqlConnection(connectionString))
{
var allResults = cn.Query<User>("SELECT UserName, Email, Phone, (SELECT TOP 1 PhotoPath FROM Photo WHERE User.UserId = Photo.UserId) FROM User)
Your User class does not contain property like PhotoPath - where you expect Dapper put will new/additional value to?
You should create new class (ViewModels/UserAndPhoto.cs for example), which contains all properties you are selecting - then Dapper will read it from database successfully.

The required column was not present in the results of a 'FromSql' operation

I've just starting learning MVC6 with EF7. I have a stored proc that I'd like to return a portion of the fields that are in my model. If I don't return every field in my model, I'm getting "The required column 'FirstName' was not present in the results of a 'FromSql' operation".
Is there a way to get make some columns not required so I can return just a portion of the fields in my model?
model:
public class LoginViewModel
{
[Key]
public int UserID { get; set; }
[Required]
[Display(Name = "Username")]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Protected ID")]
public string ProtectedID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
My proc for testing:
CREATE PROCEDURE [dbo].[aaa_TopXXUsersTest]
#NumToReturn int = 10
AS
BEGIN
SET NOCOUNT ON;
select top(#NumToReturn) UserID, LastName, Username,Password, ProtectedID from Users where Deleted = 0
END
and last, my controller code:
public IActionResult Index()
{
var user = _context.Set<LoginViewModel>().FromSql("dbo.aaa_TopXXUsersTest #NumToReturn = {0}", 20);
return View(user);
}
If I include all the fields of my model in my stored proc the call work fine, but I can't seem to return just a subset. Is there a way to make some of the fields not required?
Used [NotMapped] attribute with first name.
The NotMappedattribute can be applied to properties of an entity class for which we do not want to create corresponding columns in the database.
public class LoginViewModel
{
[Key]
public int UserID { get; set; }
[Required]
[Display(Name = "Username")]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Protected ID")]
public string ProtectedID { get; set; }
[NotMapped]
public string FirstName { get; set; }
public string LastName { get; set; }
}
this means that the column 'FirstName' is not being returned in the result set.
Do a 'SELECT * FROM TABLE' to solve the issue.
It requires a Id column to be returned from the SP.
select top(#NumToReturn) 0 AS 'Id', UserID, LastName, Username,Password, ProtectedID
from Users
where Deleted = 0
Or Change UserID to Id from select and model
Try removing this public string FirstName { get; set; }. Your stored procedure is not returning the value for Firstname field, but you are trying to accept it in your loginViewModel class in this string Firstname variable.
To addition current answers:
According to last version, FromSql changed. Instead of that we can use eather FromSqlRaw or FromSqlInterpolated
As mentioned in docs: FromSqlInterpolated is similar to FromSqlRaw but allows you to use string interpolation syntax. Just like FromSqlRaw, FromSqlInterpolated can only be used on query roots.
We can achieve excluding property from mapping, using OnModelCreating method:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<Property>(); //property needed to be excluded
}
or: [NotMapped] attribute

"[Required]" Data Annotation Not Enforced on POST Methods

I had the idea to use Data Annotations in order to validate ModelState. This works wonderfully. The problem I am having is that the [Required] Data Annotation is being enforced on [Key] fields on post. Our data layer takes care of setting Id's and we don't want anyone consuming the service to have to worry about Id's. Is there a way around this in WebApi2?
I have looked at this question, and removing the Id field from ModelState in the POST method before checking for valid ModelState would work. The issue with that is that we use a filter for ModelState.
EDIT:
After doing some more research, what I am essentially wanting to do is what the [Bind] attribute does in MVC. After some research, it does not look like this is a feature that has yet been implemented in WebApi. If anyone has any ideas, feel free to post them.
What you can do is to replace your entity with a data transfer object, which is identical to your original entity without the ID field. For example,
The original entity may look like this
public class User
{
[Required]
public Guid UserId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Username { get; set; }
public string Email { get; set; }
}
and the DTO may look like this
public class UserDto
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Username { get; set; }
public string Email { get; set; }
}
Hope this helps.

raven query on object type throws exception

document Structure:
class UserAccountInfo
{
public String Id { get; set; }
public AccountType AccountType { get; set; }Servicetax
public String MainAccountMobileNo { get; set; }
public UserStatus Status { get; set; }
public String EmailId { get; set; }
public String DisplayName { get; set; }
**public Object User { get; set; }**
}
object stores instance of any type that is mentioned in Account type. the type that is stored in the object can be found using Accountype for ex; if Accountype is customer then instance stored in the object will be AccountinfoCustomer and so on. So using that I've tried to query but getting the following exception from raven.
var Result = sess.Query<UserAccountInfo>().Where(x => x.AccountType == usertype && ((AccountInfoCustomer)x.User).Customerstatus == CustomerStatus.Pending);
{"Url: \"/indexes/dynamic/UserAccountInfos?query=AccountType%253ADistributor%2520AND%2520User).Customerstatus%253APending&start=0&pageSize=128&aggregation=None\"\r\n\r\nSystem.ArgumentException: The field ')_Customerstatus' is not indexed, cannot query on fields that are not indexed\r\n at Raven.Database.Indexing.Index.IndexQueryOperation.AssertQueryDoesNotContainFieldsThatAreNotIndexes()
This should work. Tested in current stable RavenDB 2.0.2230.
Tests here: https://gist.github.com/4692351
Are you on an older version?

Resources